/*
 *  Copyright 2009 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.livew.framework.generator;

import static org.mybatis.generator.internal.util.messages.Messages.getString;

import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

/**
 * 
 * @author Jeff Butler
 * 
 */
public class MyXMLMapperGenerator extends XMLMapperGenerator {

	public MyXMLMapperGenerator() {
		super();
	}

	protected XmlElement getSqlMapElement() {
		FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
		progressCallback.startTask(getString("Progress.12", table.toString())); //$NON-NLS-1$
		XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
		String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
		answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
				namespace));

		context.getCommentGenerator().addRootComment(answer);

		addResultMapWithoutBLOBsElement(answer);
		addResultMapWithBLOBsElement(answer);
		// addExampleWhereClauseElement(answer);
		// addMyBatis3UpdateByExampleWhereClauseElement(answer);
		addBaseColumnListElement(answer);
		addBlobColumnListElement(answer);
		// addSelectByExampleWithBLOBsElement(answer);
		// addSelectByExampleWithoutBLOBsElement(answer);
		addSelectByPrimaryKeyElement(answer);
		addDeleteByPrimaryKeyElement(answer);
		// addDeleteByExampleElement(answer);
		addInsertElement(answer);
		addInsertSelectiveElement(answer);
		// addCountByExampleElement(answer);
		// addUpdateByExampleSelectiveElement(answer);
		// addUpdateByExampleWithBLOBsElement(answer);
		// addUpdateByExampleWithoutBLOBsElement(answer);
		addUpdateByPrimaryKeySelectiveElement(answer);
		addUpdateByPrimaryKeyWithBLOBsElement(answer);
		addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
		addSelectAllElement(answer);
		addSelectByIdListElement(answer);
		addSelectListByConditionElement(answer);
		addSelectPaginationElement(answer);

		return answer;
	}

	protected void addSelectAllElement(XmlElement parentElement) {
		AbstractXmlElementGenerator elementGenerator = new MySelectAllElementGenerator();
		initializeAndExecuteGenerator(elementGenerator, parentElement);
	}

	protected void addSelectByIdListElement(XmlElement parentElement) {
		AbstractXmlElementGenerator elementGenerator = new MySelectByIdListElementGenerator();
		initializeAndExecuteGenerator(elementGenerator, parentElement);
	}

	protected void addSelectByPrimaryKeyElement(XmlElement parentElement) {
		if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
			AbstractXmlElementGenerator elementGenerator = new MySelectByPrimaryKeyElementGenerator();
			initializeAndExecuteGenerator(elementGenerator, parentElement);
		}
	}

	protected void addSelectPaginationElement(XmlElement parentElement) {
		AbstractXmlElementGenerator elementGenerator = new MySelectPaginationElementGenerator();
		initializeAndExecuteGenerator(elementGenerator, parentElement);
	}
	
	protected void addSelectListByConditionElement(XmlElement parentElement) {
		AbstractXmlElementGenerator elementGenerator = new MySelectListByConditionElementGenerator();
		initializeAndExecuteGenerator(elementGenerator, parentElement);
	}
}
